package com.gtq.uhome.service.serviceImpl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.gtq.uhome.mapper.DeliveryMapper;
import com.gtq.uhome.mapper.DeliveryPortOrderMapper;
import com.gtq.uhome.service.DeliveryPortOrderService;
import com.gtq.uhome.vo.OrderBasicInformation;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author ylhy
 * @Describe
 * @create 2020-08-08 15:36
 */
@Service("deliveryPortOrderService")
public class DeliveryPortOrderServiceImpl implements DeliveryPortOrderService {

    @Resource
    DeliveryPortOrderMapper deliveryPortOrderMapper;


    /**
     * 待取货订单列表
     * @param staffId
     * @return
     */
    @Override
    public List<OrderBasicInformation> pickUpOrderList(Integer staffId) {
        List<OrderBasicInformation> pickUpOrderList = deliveryPortOrderMapper.pickUpOrderList(staffId);
        System.out.println(pickUpOrderList+"----------------");
        List<OrderBasicInformation> newList = new ArrayList<OrderBasicInformation>();
        for (OrderBasicInformation orderBasicInformation : pickUpOrderList) {
            if (orderBasicInformation.getOrderId() != null || orderBasicInformation.getSiteName() != null || orderBasicInformation.getConsigneeName() != null || orderBasicInformation.getDeliveryTime() != null){
                newList.add(orderBasicInformation);
            }
        }
        if (newList.size() != 0){
            return newList;
        }else {
            return null;
        }
    }

    /**
     * 确认取货
     * @param orderId
     * @return
     */
    @Override
    @Transactional
    public Integer confirmReceipt(String orderId,Integer userId,String consigneeName) {
        Integer lines = deliveryPortOrderMapper.confirmReceipt(orderId);
        String infor ="尊敬的用户,您的"+orderId+"订单,配送人员已经取货,即将发往"+consigneeName;
        if (lines == 1){
            Integer line = deliveryPortOrderMapper.setPickTime(orderId,
                    new Timestamp(Calendar.getInstance().getTimeInMillis()));
            System.out.println("---------"+line+"-----------");
            if (line == 1){
                deliveryPortOrderMapper.sendMsg(userId,infor,new Timestamp(Calendar.getInstance().getTimeInMillis()));
            }
        }
        return lines;
    }

    /**
     * 待送货列表
     * @param staffId
     * @return
     */
    @Override
    public List<OrderBasicInformation> waitSendOrderList(Integer staffId) {
        List<OrderBasicInformation> waitSendOrderList = deliveryPortOrderMapper.waitSendOrderList(staffId);

        List<OrderBasicInformation> newList = new ArrayList<OrderBasicInformation>();
        for (OrderBasicInformation or : waitSendOrderList) {
            if (or.getOrderId() != null || or.getSiteName() !=
                    null || or.getConsigneeName() != null || or.getDeliveryTime() != null || or.getPickTime() != null){
                System.out.println(or);
                newList.add(or);
            }
        }
        waitSendOrderList = newList;
        Timestamp beginningTime = Timestamp.valueOf("1970-01-01 08:00:00");
        System.out.println(waitSendOrderList);
        if(waitSendOrderList != null){
            for (OrderBasicInformation orderBasicInformation : waitSendOrderList) {
                System.out.println(orderBasicInformation);
                //现在的时间搓
                Timestamp timestampNow = new Timestamp(Calendar.getInstance().getTimeInMillis());
                Timestamp timestampEnd = null;
                //最晚送达的时间搓
                if (orderBasicInformation.getDeliveryTime() !=null){
                    timestampEnd =
                            new Timestamp(((orderBasicInformation.getDeliveryTime().getTime() + (48 * 60 * 60 * 1000))));
                }
                System.out.println(timestampEnd+" :  结束时间");
                //剩余时间的时间搓
                Timestamp timestampLast = new Timestamp(timestampEnd.getTime() - timestampNow.getTime());
                System.out.println(timestampLast+" : 剩余时间的时间搓");

                String time = "";
                if (beginningTime.getTime() > timestampLast.getTime()){
                    orderBasicInformation.setLastSendTime("数据错误");
                }else {
                    time = timestampLast.toString();
                    time = time.substring(11,16);
                }
                orderBasicInformation.setLastSendTime(time);
                System.out.println(orderBasicInformation.getLastSendTime()+"插入的");
            }
            System.out.println(waitSendOrderList);
            return waitSendOrderList;
        }else {
            System.out.println("----------------fsdgfdfgvadfbvgc--------------");
            return null;
        }


    }


    /**
     * 确认签收
     * @param map
     * @return
     */
    @Override
    public Integer confirmSign(Map<String,Object> map) {
        String orderId = (String) map.get("orderId");
        String signStatus = (String) map.get("signStatus");
        String signIn = (String) map.get("signIn");
        Integer userId = (Integer) map.get("userId");
        String consigneeName = (String) map.get("consigneeName");
        String infor = "";
        map.put("now",new Timestamp(Calendar.getInstance().getTimeInMillis()));
        Integer line = null;
        if (line == null){
            line = deliveryPortOrderMapper.confirmSignFromOrder(orderId,signStatus);
            if (line == 1){
                Integer lineDe = deliveryPortOrderMapper.confirmSignFromDe(map);
                if (lineDe == 1){
                    infor ="尊敬的用户,您发给"+consigneeName+"的"+orderId+"订单,已经确认签收";
                    deliveryPortOrderMapper.sendMsg(userId,infor,new Timestamp(Calendar.getInstance().getTimeInMillis()));
                }else {
                    infor ="尊敬的用户,您发给"+userId+"的"+orderId+"订单,签收失败";
                    deliveryPortOrderMapper.sendMsg(userId,infor,new Timestamp(Calendar.getInstance().getTimeInMillis()));
                }
            }
            return line;
        }else {
            return line;
        }


    }

    /**
     * 成功订单列表
     * @param staffId
     * @return
     */
    @Override
    public List<OrderBasicInformation> okOrderList(Integer staffId) {

        List<OrderBasicInformation> okOrderList = deliveryPortOrderMapper.okOrderList(staffId);
        List<OrderBasicInformation> list = new ArrayList<>();
        for (OrderBasicInformation ok : okOrderList) {
            if (ok.getOrderId() != null || ok.getSiteName() != null || ok.getConsigneeName() != null || ok.getConsigneeId() != null || ok.getDeliveryTime() !=null || ok.getPickTime() !=null){
                list.add(ok);
            }
        }
        return list;
    }

    /**
     * 查询失败订单列表
     * @param staffId
     * @return
     */
    @Override
    public List<OrderBasicInformation> failedOrderList(String staffId) {
        List<OrderBasicInformation> failedOrderList = deliveryPortOrderMapper.failedOrderList(staffId);
        return failedOrderList;
    }

    /**
     * 退回订单操作
     * @param orderId
     * @return
     */
    @Override
    @Transactional
    public Integer returnOrder(String orderId,Integer userId,String consigneeName) {
        Integer returnOrder =  deliveryPortOrderMapper.returnOrder(orderId);
        String infor ="尊敬的用户,您发给"+consigneeName+"的"+orderId+"订单,已退单";
        if (returnOrder ==1){
            returnOrder =  deliveryPortOrderMapper.returnOrderFromDe(orderId);
            if (returnOrder == 1){
                deliveryPortOrderMapper.sendMsg(userId,infor,new Timestamp(Calendar.getInstance().getTimeInMillis()));
            }
        }
        return returnOrder;
    }
}
